import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
const Layout = ()=>import("@/views/Layout.vue")
const Home = ()=>import("@/views/home/Home.vue")
const Category =()=>import("@/views/category/Category.vue")
const Cart = ()=>import("@/views/cart/Cart.vue")
const User =()=>import("@/views/user/User.vue")
const Login =()=>import("@/views/Login.vue")
const List =()=>import("@/components/Produce/List.vue")
const Goods =()=>import("@/components/Produce/Goods.vue")
const Address =()=>import("@/views/address/Address.vue")
const Favorite =()=>import("@/views/favorite/Favorite.vue")
const Set =()=>import("@/views/set/Set.vue")
const AddressManage =()=>import("@/views/address/components/EditForm.vue")
const GoodsOrder =()=>import("@/components/Order/GoodsOrder.vue")
const Pay = ()=>import("@/components/Order/Pay.vue")
const Order = ()=>import("@/components/Order/Order.vue")
const Detail =()=>import("@/components/Order/OrderDetail.vue")

Vue.use(VueRouter)

const routes = [
    {
      path:"/",
      redirect:"/home",
      component:Layout,
      children:[
        {
          path:"/home",
          component:Home
        },
        {
          path:"/category",
          component:Category
        },
        {
          path:"/cart",
          component:Cart
        },
        {
          path:"/user",
          component:User
        }
      ]
    },
    {
      path:'/login',
      component:Login
    },
    {
      path:"/product/list",
      name:"product",
      component:List
    },
    {
      path:"/product",
      component:Goods
    },
    {
      path:"/address",
      component:Address,
      beforeEnter: (to, from, next) => {
          if(store.state.token){
              next();
          }else{
            next("/login")
          }
      },
    },
    {
      path:"/address/addressManage",
      component:AddressManage
    },
    {
      path:"/favorite",
      component:Favorite,
    },
    {
      path:"/set",
      component:Set,
      beforeEnter: (to, from, next) => {
          if(store.state.token){
              next()
          }else{
            next("/login")
          }
      }
    },
    {
      path:"/createOrder",
      component:GoodsOrder,
      beforeEnter: (to, from, next) => {
          if(store.state.token){
              next()
          }else{
            next("/login")
          }
      }
    },
    {
      path:"/pay",
      component:Pay,
      beforeEnter: (to, from, next) => {
          if(store.state.token){
              next()
          }else{
            next("/login")
          }
      }
    },
    {
      path:"/order",
      component:Order,
      beforeEnter: (to, from, next) => {
          if(store.state.token){
              next()
          }else{
            next("/login")
          }
      }
    },
    {
      path:"/orderDetail",
      component:Detail
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
